package com.wxapp.market.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.condition.ConditionUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.market.api.pojo.entity.AdDO;
import com.wxapp.market.api.pojo.entity.AdFileDO;
import com.wxapp.market.api.pojo.vm.ad.PAdAddFilesVm;
import com.wxapp.market.api.pojo.vm.ad.PAdDetailVm;
import com.wxapp.market.api.pojo.vm.ad.PAdPageVm;
import com.wxapp.market.api.pojo.vm.ad.PAdSelectVm;
import com.wxapp.market.api.pojo.vo.ad.QAdAddVo;
import com.wxapp.market.api.pojo.vo.ad.QAdPageVo;
import com.wxapp.market.api.pojo.vo.ad.QAdUpdateVo;
import com.wxapp.market.service.mapper.AdFileMapper;
import com.wxapp.market.service.mapper.AdMapper;
import com.wxapp.market.service.service.AdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class AdServiceImpl extends IBaseService implements AdService {
    private final AdMapper adMapper;
    private final AdFileMapper adFileMapper;

    @Override
    public ResultVm<String> add(QAdAddVo qAdAddVo, UserVo userVo) {
        var adDO = CopyUtil.copyObjectNew(qAdAddVo, AdDO::new, (t, s) -> {
            s.setCreateBy(userVo.getId());
            return null;
        });
        adMapper.insert(adDO);
        return success("保存成功");
    }

    @Override
    public ResultVm<String> update(QAdUpdateVo qAdUpdateVo, UserVo userVo) {
        var adDO = adMapper.selectById(qAdUpdateVo.getId());
        if (CheckUtil.isNullObject(adDO)) {
            return error("没有查到推广消息");
        }
        CopyUtil.copyObject(qAdUpdateVo, adDO, ((t, s) -> {
            s.setUpdateBy(userVo.getId());
            return null;
        }));
        adMapper.updateById(adDO);
        return success("更新成功");
    }

    @Override
    public ResultVm<Page<PAdPageVm>> getPage(QAdPageVo qAdPageVo) {
        Map<SFunction<AdDO, ?>, SqlKeyword> conditions = new HashMap<>();
        conditions.put(AdDO::getName, SqlKeyword.LIKE);
        var page = ConditionUtil.getPage(qAdPageVo, AdDO.class);
        var condition = ConditionUtil.getSCondition(AdDO::new, qAdPageVo, PAdPageVm.class, conditions,null);
        var adDOPage = adMapper.selectPage(page, condition);
        return success(CopyUtil.copyPage(adDOPage, PAdPageVm::new, null));
    }

    @Override
    public ResultVm<String> delete(String id) {
        var adDO = adMapper.selectById(id);
        if (CheckUtil.isNullObject(adDO)) {
            return error("没有查到推广消息");
        }
        adDO.setStatus(false);
        adMapper.updateById(adDO);
        return success("删除成功");
    }

    @Override
    public ResultVm<PAdDetailVm> detail(String id) {
        var adDO = adMapper.selectById(id);
        if (CheckUtil.isNullObject(adDO)) {
            return error("没有查到推广消息");
        }
        return success(CopyUtil.copyObjectNew(adDO, PAdDetailVm::new, null));
    }


    @Override
    public ResultVm<PAdDetailVm> getList(String position) {
        if (CheckUtil.isNullEnum(position)) {
            return error("请输入正确的位置代码");
        }
        var query = new LambdaQueryWrapper<AdDO>();
        query.eq(AdDO::getPosition, position)
                .eq(AdDO::getStatus, true);
        var ads = adMapper.selectFirst(query);
        if (CheckUtil.isNullObject(ads)) {
            return success();
        }
        var fileQuery = new LambdaQueryWrapper<AdFileDO>();
        fileQuery.eq(AdFileDO::getAdId, ads.getId())
                .eq(AdFileDO::getStatus, true)
                .orderByAsc(AdFileDO::getSort);
        var adFileS = adFileMapper.selectList(fileQuery);
        PAdDetailVm pAdDetailVm = CopyUtil.copyObjectNew(ads, PAdDetailVm::new, null);
        if (CheckUtil.isNullList(adFileS)) {
            return success(pAdDetailVm);
        }
        pAdDetailVm.setFiles(CopyUtil.copyListNew(adFileS, PAdAddFilesVm::new, null));
        return success(pAdDetailVm);

    }

    @Override
    public ResultVm<List<PAdSelectVm>> getSelect(boolean allData, String name) {
        if (CheckUtil.isNullString(name) && !allData){
            return success(new ArrayList<>());
        }
        var query = new LambdaQueryWrapper<AdDO>();
        query.select(AdDO::getId, AdDO::getName)
                .like(CheckUtil.isNotNullString(name),AdDO::getName, name)
                .eq(AdDO::getStatus, true);
        var adFileS = adMapper.selectList(query);
        return success(CopyUtil.copyListNew(adFileS, PAdSelectVm::new, null));
    }
}
